
layui.config({
	base : 'js/'
}).use([ 'element', 'layer', 'navbar', 'tab' ], function() {
	var element = layui.element(),
		$ = layui.jquery,
		layer = layui.layer,
		navbar = layui.navbar(),
		tab = layui.tab({
			elem : '.admin-nav-card' //设置选项卡容器
		});

	//获取cookie中的用户名
	var username = readCookie("userName"),
		//密码错误次数
		i = 5;

	if (username == '' || username == null) {
		window.location.href = "view/login.html";
	}
	//时间属性，防止浏览器缓存
	var timestamp = new Date().getTime();
	//获取用户信息	
	$.ajax({
		url : 'getUserByName/' + username,
		type : "GET",
		async : false,
		success : function(info) {
			$('#name1').html(info.name);
			$('#img1').attr("src", info.img + '?' + timestamp);
		}
	});

	//iframe自适应
	$(window).on('resize', function() {
		var $content = $('.admin-nav-card .layui-tab-content');
		$content.height($(this).height() - 147);
		$content.find('iframe').each(function() {
			$(this).height($content.height());
		});
	}).resize();

	//设置navbar
	navbar.set({
		spreadOne : true,
		elem : '#admin-navbar-side',
		cached : true,
		data : navs
	});
	//渲染navbar
	navbar.render();
	//监听点击事件
	navbar.on('click(side)', function(data) {
		tab.tabAdd(data.field);
	});

	//给编辑个人信息按钮设置监听
	$('#editself').on('click', function() {
		tab.tabAdd({
			"title" : "个人信息",
			"icon" : "&#xe612;",
			"href" : "temp/edit-self.html"
		});
	});

	$('.admin-side-toggle').on('click', function() {
		var sideWidth = $('#admin-side').width();
		if (sideWidth === 200) {
			$('#admin-body').animate({
				left : '0'
			}); //admin-footer
			$('#admin-footer').animate({
				left : '0'
			});
			$('#admin-side').animate({
				width : '0'
			});
		} else {
			$('#admin-body').animate({
				left : '200px'
			});
			$('#admin-footer').animate({
				left : '200px'
			});
			$('#admin-side').animate({
				width : '200px'
			});
		}
	});

	//锁屏
	$(document).on('keydown', function() {
		var e = window.event;
		if (e.keyCode === 76 && e.altKey) {
			alert("你按下了alt+l");
			lock($, layer);
		}
	});
	$('#lock').on('click', function() {
		lock($, layer);
	});

	//手机设备的简单适配
	var treeMobile = $('.site-tree-mobile'),
		shadeMobile = $('.site-mobile-shade');
	treeMobile.on('click', function() {
		$('body').addClass('site-mobile');
	});
	shadeMobile.on('click', function() {
		$('body').removeClass('site-mobile');
	});

	function lock($, layer) {
		//自定页
		layer.open({
			title : false,
			type : 1,
			closeBtn : 0,
			anim : 6,
			content : $('#lock-temp').html(),
			shade : [ 0.9, '#393D49' ],
			success : function(layero, lockIndex) {
				//修改弹窗的头像
				layero.find('img#img2').attr("src", $('#img1').attr("src") + '?' + timestamp);
				//给显示用户名赋值
				layero.find('div#lockUserName').text(username);
				layero.find('input[name=lockPwd]').on('focus', function() {
					var $this = $(this);
					if ($this.val() === '输入密码解锁..') {
						$this.val('').attr('type', 'password');
					}
				})
					.on('blur', function() {
						var $this = $(this);
						if ($this.val() === '' || $this.length === 0) {
							$this.attr('type', 'text').val('输入密码解锁..');
						}
					});

				//删除认证
				$.ajax({
					url : 'lockout',
					async : false,
				});

				//绑定解锁按钮的点击事件
				layero.find('button#unlock').on('click', function() {
					var $lockBox = $('div#lock-box');

					var pwd = $lockBox.find('input[name=lockPwd]').val();
					if (pwd === '输入密码解锁..' || pwd.length === 0) {
						layer.msg('请输入密码..', {
							icon : 2,
							time : 1000
						});
						return;
					}
					unlock(username, pwd);
				});
				/**
				 * 解锁操作方法
				 * @param {String} 用户名
				 * @param {String} 密码
				 */
				var unlock = function(username, password) {
					//解锁
					var json = {
						'username' : username,
						'password' : password
					};

					$.ajax({
						url : 'relogin',
						type : "POST",
						data : JSON.stringify(json),
						async : false,
						dataType : 'json',
						contentType : 'application/json;charset=utf-8',
						success : function(info) {
							if (info.message.indexOf('login success') != -1) {
								//关闭锁屏层
								layer.close(lockIndex);
							} else if (info.message.indexOf("error password") != -1) {
								layer.msg('密码错误!还可尝试' + --i + '次!');
								flushImg();
							} else if (info.message.indexOf("error code") != -1) {
								layer.msg('验证码错误!');
								flushImg();
							} else if (info.message.indexOf("locked user") != -1) {
								layer.msg('您的账号已被锁定!');
							} else if (info.message.indexOf("locked 10min") != -1) {
								layer.msg('密码错误,您的账号被锁定10分钟!');
								flushImg();
							}
						}
					});
				};
			}
		});
	}

	function readCookie(name) {
		var cookieValue = "";
		var search = name + "=";
		if (document.cookie.length > 0) {
			offset = document.cookie.indexOf(search);
			if (offset != -1) {
				offset += search.length;
				end = document.cookie.indexOf(";", offset);
				if (end == -1)
					end = document.cookie.length;
				cookieValue = (document.cookie.substring(offset, end))
			}
		}
		return cookieValue;
	}

});